package com.cgc.venus.server.app.service;

import com.cgc.venus.server.app.inter.AppStorageService;
import com.cgc.venus.server.core.base.AbstractBaseService;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.k8s.impl.K8sStorageServiceImpl;
import com.cgc.venus.server.core.k8s.inter.K8sStorageService;
import com.cgc.venus.server.core.utils.StringUtil;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 应用存储管理功能模块
 * Created by 业哥 on 2017/9/20.
 */
@Service
public class AppStorageServiceImpl extends AbstractBaseService implements AppStorageService {
    @Autowired
    private K8sStorageService k8sStorageServiceImpl;
    @Override
    public Object getPersistentVolumeClaims(String namespace, String queryKey)throws VenusException {
        List<PersistentVolumeClaim> list= k8sStorageServiceImpl.getPersistentVolumeClaims(namespace);
        if(!StringUtil.isEmpty(queryKey)){
            list=list.stream().filter(ts -> ts.getMetadata().getName().indexOf(queryKey)!=-1).collect(Collectors.toList());
        }
        List<Map<String,String>> maps=new ArrayList<Map<String,String>>();
        list.forEach(pvc->{
            Map<String,String> map=new HashMap<String, String>();
            maps.add(map);
            map.put("time", pvc.getMetadata().getCreationTimestamp().replaceAll("T"," ").replaceAll("Z","").split(" ")[0]);
            map.put("name",pvc.getMetadata().getName());
            map.put("status",pvc.getStatus().getPhase());
            map.put("type","nfs");
            if(pvc.getMetadata().getAnnotations()!=null){
                map.put("mountPath",pvc.getMetadata().getAnnotations().get(K8sStorageServiceImpl.ANNOTATION_KYE_MOUNT_PATH));
                map.put("appName",pvc.getMetadata().getAnnotations().get(K8sStorageServiceImpl.ANNOTATION_KYE_APP));
            }else{
                map.put("mountPath","");
                map.put("appName","");
            }
            map.put("namespace",pvc.getMetadata().getNamespace());
            map.put("size",pvc.getSpec().getResources().getRequests().get("storage").getAmount());
            if(pvc.getStatus().getAccessModes().size()>0){
                map.put("accessMode",pvc.getStatus().getAccessModes().get(0));
            }else{
                map.put("accessMode","");
            }
        });
        return maps;
    }

    @Override
    public Object getPersistentVolumeClaimsAvailable(String namespace) throws VenusException {
        List<PersistentVolumeClaim> list= k8sStorageServiceImpl.getPersistentVolumeClaims(namespace);
        List<Map<String,String>> maps=new ArrayList<Map<String,String>>();
        list.forEach(pvc->{
            if(pvc.getMetadata().getAnnotations()==null||StringUtil.isEmpty(pvc.getMetadata().getAnnotations().get(K8sStorageServiceImpl.ANNOTATION_KYE_APP))){
                Map<String,String> map=new HashMap<String, String>();
                map.put("name",pvc.getMetadata().getName());
                map.put("size",pvc.getSpec().getResources().getRequests().get("storage").getAmount());
                maps.add(map);
            }
        });
        return maps;
    }

    @Override
    public Object deletePersistentVolumeClaims(String namespace, String pvcName) throws VenusException {
        PersistentVolumeClaim persistentVolumeClaim=k8sStorageServiceImpl.getPersistentVolumeClaim(pvcName,namespace);
        if(persistentVolumeClaim==null){
            throw new VenusException(VenusResponseStatus.K8S_EXCEPTION,"删除存储失败");
        }
        return k8sStorageServiceImpl.deletePersistentVolumeClaims(persistentVolumeClaim);
    }

    @Override
    public Boolean createPersistentVolumeClaims(String pvcName, String namespace, String pvcAccessMode, String reqStorageSize) throws VenusException {
        return k8sStorageServiceImpl.createPersistentVolumeClaims(pvcName,namespace,pvcAccessMode,reqStorageSize);
    }

    @Override
    public Boolean checkStorageClaimName(String pvcName, String namespace) throws VenusException {
        PersistentVolumeClaim persistentVolumeClaim=k8sStorageServiceImpl.getPersistentVolumeClaim(pvcName,namespace);
        if(persistentVolumeClaim!=null){
            return true;
        }else{
            return false;
        }
    }
}